package com.example.mobile2021606129ly0401;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
//点击开灯，显示亮的图片。点击关灯，显示暗的图片。
//点击按钮，如果当前是转动状态，则停止
//点击按钮，如果当前是停止状态，则转动
public class MainActivity extends AppCompatActivity {

    private ImageView imalight;
    private Button butOn;
    private Button butOff;
    private Button button;

    private  boolean fan=false;//当一个状态变量

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        initView();
     button.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             imalight.setImageResource(R.drawable.fun_on);
           if (fan) {
               imalight.setImageResource(R.drawable.fun_off);
               fan =false ;
           }else {
               imalight.setImageResource(R.drawable.fun_on);
               fan=false;
           }

     }
     });

     butOn.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             imalight.setImageResource(R.drawable.light_on);
         }
     });

     butOff.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             imalight.setImageResource(R.drawable.light_off);
         }
     });
    }


    private void initView() {
        imalight = (ImageView) findViewById(R.id.ima_light);
        butOn = (Button) findViewById(R.id.but_on);
        butOff = (Button) findViewById(R.id.but_off);
        button = (Button) findViewById(R.id.button);
    }
}